package android.material.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.Rect;
import android.material.util.Util;
import android.support.annotation.NonNull;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.Button;

import hd.util.ScreenHelper;

/**
 * 没有阴影只有波纹的Button
 * <p/>
 * 它不管背景是什么颜色，只要按下去有反馈效果就行了
 * <p/>
 * Created by haidy on 15/1/1.
 */
public class ActionButton extends Button {

    private static final short ANIMATION_DURATION = 200;
    private final Paint paint;
    private boolean pressed = false;//当前是否按下
    private boolean animStop = true;//动画是否停止
    private int circleRadius = 0;//当前圆的半径
    private long animStartTime;
    private Point point = new Point();
    private final int ANIM_RATE = ScreenHelper.dp2px(getContext(), 40); //动画的速率
    private final int ANIM_ACC_RATE = ScreenHelper.dp2px(getContext(), 2);//失去焦点加速的速率
    private int currentAnimRate = 0;//当前速率
    private final int alpha = 0x20;//透明度
    private final int backgroundAlpha = 0x15;//透明度
    private int currentAlpha = 0;
    private int currentBackgroundAlpha = 0;
    private Rect rect;

    public ActionButton(Context context) {
        this(context, null);
    }

    public ActionButton(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ActionButton(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        setBackgroundColor(Color.TRANSPARENT);
        paint = new Paint();
        paint.setColor(Color.rgb(0, 0, 0));
        paint.setStyle(Paint.Style.FILL);
        paint.setAntiAlias(true);
    }

    @Override
    public boolean onTouchEvent(@NonNull MotionEvent event) {
        rect = new Rect(0, 0, getWidth(), getHeight());
        super.onTouchEvent(event);
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                pressed = true;//按下
                point.x = (int) event.getX();
                point.y = (int) event.getY();//记录按下的位置
                animStop = false;
                circleRadius = 0;
                animStartTime = System.currentTimeMillis();//记录开始的时间
                currentAnimRate = ANIM_RATE;
                currentBackgroundAlpha = 0;
                paint.setAlpha(currentAlpha = alpha);
                invalidate();
                break;
            case MotionEvent.ACTION_MOVE:
                Point p = new Point((int) event.getX(), (int) event.getY());
                if (!Util.inRect(p, rect)) {
                    actionUp();
                    return false;
                }
                break;
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_CANCEL://取消和抬起是一个效果
                actionUp();
                break;
        }
        return true;
    }

    private void actionUp() {
        postDelayed(new Runnable() {
            @Override
            public void run() {
                pressed = false;//抬起
                invalidate();//快速的消失波纹
            }
        }, 100);
    }

    @Override
    protected void onDraw(@NonNull Canvas canvas) {
        super.onDraw(canvas);
        if (pressed) {
            if (Util.circleInRect(circleRadius, point, rect)) {
                if (currentBackgroundAlpha < backgroundAlpha)
                    currentBackgroundAlpha++;
                canvas.drawColor(Color.argb(currentBackgroundAlpha, 0, 0, 0));
                long time = System.currentTimeMillis() - animStartTime;
                circleRadius = Math.round(time * ANIM_RATE / ANIMATION_DURATION);
                paint.setAlpha(currentAlpha);
                canvas.drawCircle(point.x, point.y, circleRadius, paint);
                invalidate();
            } else {//画出最后一帧结束
                canvas.drawColor(Color.argb(currentBackgroundAlpha, 0, 0, 0));
                canvas.drawCircle(point.x, point.y, circleRadius, paint);
                animStop = true;
            }
        } else {//抬起或者取消则快速扩大半径，直到充满结束
            if (animStop) {
                if (currentAlpha > 0) {
                    paint.setAlpha(currentAlpha -= 2);//透明度变化的速率
//                    canvas.drawCircle(point.x, point.y, circleRadius, paint);
                    canvas.drawPaint(paint);
                    if (currentBackgroundAlpha > 0)
                        canvas.drawColor(Color.argb(currentBackgroundAlpha -= 1, 0, 0, 0));
                    invalidate();
                } else {
                    paint.setAlpha(0);
                    canvas.drawCircle(point.x, point.y, circleRadius, paint);
                }
            } else {//动画还没消失，就快速扩大到最大半径
                if (Util.circleInRect(circleRadius, point, rect)) {
                    long time = System.currentTimeMillis() - animStartTime;
                    currentAnimRate += ANIM_ACC_RATE;//松手时加快的速率，越大扩散速度越快
                    circleRadius = Math.round(time * currentAnimRate / ANIMATION_DURATION);
                    paint.setAlpha(currentAlpha);
                    canvas.drawColor(Color.argb(currentBackgroundAlpha, 0, 0, 0));
                    canvas.drawCircle(point.x, point.y, circleRadius, paint);
                    invalidate();
                } else {//画出最后一帧结束
                    canvas.drawColor(Color.argb(currentBackgroundAlpha, 0, 0, 0));
                    canvas.drawCircle(point.x, point.y, circleRadius, paint);
                    animStop = true;
                    invalidate();
                }
            }
        }
    }
}
